package com.sgb.kjwl.viewmodel.self.publish;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.model.entity.user.AddTalentEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.publish.PersonPublishNextActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.io.File;
import java.util.List;

import io.reactivex.functions.Consumer;

public class PersonnelNextModel extends BaseViewModel {
    public ObservableField<AddTalentEntity> addTalentEntity = new ObservableField<>(new AddTalentEntity());
    private PersonPublishNextActivity activity;
    private SelfRepertory repertory;

    public PersonnelNextModel(PersonPublishNextActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    //增加发布人才
    public void addPersonnelModel() {
        DialogHelper.showProgressDialog(activity, null, "数据提交中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.addTalent(addTalentEntity.get())
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showSuccess();
                        } else {
                            MToast.showToast(activity, baseEntity.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //编辑发布人才
    public void editPersonnelModel() {
        DialogHelper.showProgressDialog(activity, null, "数据提交中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.editTalent(addTalentEntity.get())
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            activity.showSuccess();
                        } else {
                            MToast.showToast(activity, baseEntity.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    //上传多图片
    public void uploadMutiFileModel(List<File> fileList,List<String> netUrls) {
        DialogHelper.showProgressDialog(activity, null, "图片上传中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.uploadMutiFile(fileList)
                .subscribe(new Consumer<BaseEntity<List<String>>>() {
                    @Override
                    public void accept(BaseEntity<List<String>> baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        activity.uploadImgSuccess(baseEntity.getData(), netUrls);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }
}
